package com.kevin.sportplay.controller;

import com.alibaba.fastjson.JSON;
import com.kevin.sportplay.bean.Page;
import com.kevin.sportplay.bean.User;
import com.kevin.sportplay.dao.UserDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @author KevinYE
 * @creat 2021-08-11-13:04
 */
@RestController
public class UserController {

    @Autowired
    UserDao userDao;

    @RequestMapping("/allUser")
    public String getUserList(Page page){

        // 用户数量
        int userCounts = userDao.getUserCounts(page.getUsername());
        // 当前页
        int pageStart = (page.getPageStart()-1)  *  page.getPageSize();

        List<User> users = userDao.getAllUser(page.getUsername(), pageStart, page.getPageSize());

        HashMap<String, Object> userMap = new HashMap<>();

        userMap.put("userCounts",userCounts);
        userMap.put("users",users);

        String jsonString = JSON.toJSONString(userMap);
        return jsonString;
    }


    @RequestMapping("/updateState")
    public String updateUserState(@RequestParam("id") int id,@RequestParam("state") Boolean state){
        System.out.println("test:");
        int i = userDao.updateUserState(id, state);
        String str = i>0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestBody User user){

        System.out.println(user);
        System.out.println("test");

        user.setRole("普通用户");
        user.setState(false);

        int i = userDao.insertUser(user);
        return i>0 ? "success" : "error";
    }
}
